package org.opsli.modulars.business.iot.protocol;

import com.alibaba.fastjson.JSON;
import org.opsli.api.wrapper.czData.CzDataModel;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class CzsjUtil {

    public static void main(String[] args){
        /*String s="00084095565241623F90414A8CCC418301A43FB912A2BFFF51C03EC5C21AC0708370001DAB3A01C4E010000000000000000041C000004176B8E5021E5FE20A31BFBF45001CCD46251366400D70A4409B701102D1A8570C4BA54100000000000000004183C28F409BA3ADC811";
        CzDataModel czsj=getdata(s);
        System.out.println(JSON.toJSONString(czsj));*/
       /* System.out.println(getFloat("40955652"));*/
        /*System.out.println(hexFloat2BigDecimal("BFDD8700"));*/
        System.out.println(getStr("0.01"));
        System.out.println(getFloat("3c23d70a"));
    }



    /**
     * 十六进制单精度浮点数，转BigDecimal，保留2为小数，截掉多余小数位
     * @param hex
     * @return
     */
    public static BigDecimal hexFloat2BigDecimal(String hex) {
        float value = Float.intBitsToFloat((int)Long.parseLong(hex, 16));
        System.out.println(value);
        BigDecimal bd = new BigDecimal(Float.toString(value));
        return bd.setScale(2, BigDecimal.ROUND_DOWN);
    }








    //解析场站数据
    public static CzDataModel getdata(String cmd){
        CzDataModel czsj=new CzDataModel();
        czsj.setDevid("41001");
        String pt101=cmd.substring(4,12);
        String tt101=cmd.substring(12,20);
        String tt102=cmd.substring(20,28);
        String tt103=cmd.substring(28,36);
        String pt102=cmd.substring(36,44);
        String tt104=cmd.substring(44,52);
        String pt103=cmd.substring(52,60);
        String tt105=cmd.substring(60,68);

        String ft101Gksum=cmd.substring(68,76);
        String ft101Bksum=cmd.substring(76,84);
        String ft101Gk=cmd.substring(84,92);
        String ft101Bk=cmd.substring(92,100);
        String ft101Tt=cmd.substring(100,108);
        String ft101Pt=cmd.substring(108,116);

        String ft102Gksum=cmd.substring(116,124);
        String ft102Bksum=cmd.substring(124,132);
        String ft102Gk=cmd.substring(132,140);
        String ft102Bk=cmd.substring(140,148);
        String ft102Tt=cmd.substring(148,156);
        String ft102Pt=cmd.substring(156,164);

        String ft103Gksum=cmd.substring(164,172);
        String ft103Bksum=cmd.substring(172,180);
        String ft103Gk=cmd.substring(180,188);
        String ft103Bk=cmd.substring(188,196);
        String ft103Tt=cmd.substring(196,204);
        String ft103Pt=cmd.substring(204,212);



        czsj.setPt101(getFloat(pt101));
        czsj.setTt101(getFloat(tt101));
        czsj.setTt102(getFloat(tt102));
        czsj.setTt103(getFloat(tt103));
        czsj.setPt102(getFloat(pt102));
        czsj.setTt104(getFloat(tt104));
        czsj.setPt103(getFloat(pt103));
        czsj.setTt105(getFloat(tt105));

        czsj.setFt101Gksum(getInt(ft101Gksum));
        czsj.setFt101Bksum(getInt(ft101Bksum));
        czsj.setFt101Gk(getFloat(ft101Gk));
        czsj.setFt101Bk(getFloat(ft101Bk));
        czsj.setFt101Tt(getFloat(ft101Tt));
        czsj.setFt101Pt(getFloat(ft101Pt));

        czsj.setFt102Gksum(getInt(ft102Gksum));
        czsj.setFt102Bksum(getInt(ft102Bksum));
        czsj.setFt102Gk(getFloat(ft102Gk));
        czsj.setFt102Bk(getFloat(ft102Bk));
        czsj.setFt102Tt(getFloat(ft102Tt));
        czsj.setFt102Pt(getFloat(ft102Pt));

        czsj.setFt103Gksum(getInt(ft103Gksum));
        czsj.setFt103Bksum(getInt(ft103Bksum));
        czsj.setFt103Gk(getFloat(ft103Gk));
        czsj.setFt103Bk(getFloat(ft103Bk));
        czsj.setFt103Tt(getFloat(ft103Tt));
        czsj.setFt103Pt(getFloat(ft103Pt));


        return czsj;


    }


    //浮点型
    public static String getFloat(String str){
       /* int ieee754Int = Integer.parseInt(str, 16);
        float realValue = Float.intBitsToFloat(ieee754Int);
        return String.valueOf(realValue);*/
        float value = Float.intBitsToFloat((int)Long.parseLong(str, 16));
        System.out.println(value);

        return String.valueOf(value);
    }

    public static String getStr(String str){
        Float f=Float.valueOf(str);
        String value= Integer.toHexString(Float.floatToIntBits(f));
        System.out.println(value);
        return value;
    }



    //整型
    public static String getInt(String content){
        int number=0;
        String [] HighLetter = {"A","B","C","D","E","F"};
        Map<String,Integer> map = new HashMap<>();
        for(int i = 0;i <= 9;i++){
            map.put(i+"",i);
        }
        for(int j= 10;j<HighLetter.length+10;j++){
            map.put(HighLetter[j-10],j);
        }
        String[]str = new String[content.length()];
        for(int i = 0; i < str.length; i++){
            str[i] = content.substring(i,i+1);
        }
        for(int i = 0; i < str.length; i++){
            number += map.get(str[i])*Math.pow(16,str.length-1-i);
        }
        return number+"";
    }
}
